package com.example.MultiViewPage;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
		// 缓存数量为3个
		viewPager.setOffscreenPageLimit(3);
		// ViewPager每个子view的间隔
		viewPager.setPageMargin(100);

		View container = findViewById(R.id.container);
		// 需要setOnTouchListener函数中将滑动滑动事件传递给viewPager，否则只有viewPager中间的view可以滑动，设置后整个viewPager都可以滑动。
		container.setOnTouchListener(new View.OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				return viewPager.dispatchTouchEvent(event);
			}
		});

		List<String> list = new ArrayList<>();
		for (int i = 0; i < 5; i++)
		{
			list.add("第" + i + "个");
		}
		viewPager.setAdapter(new MyAdapter(list));

	}
}
